package com.ktg.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ktg.ktgcommon.domain.mainData.MdClient;
import com.ktg.ktgcommon.domain.mainData.MdWorkshop;
import com.ktg.ktgcommon.filter.TokenVerifyFilter;
import com.ktg.mapper.WorkshopMapper;
import com.ktg.service.WorkshopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class WorkshopServiceImpl implements WorkshopService {

    @Autowired
    private WorkshopMapper workshopMapper;

    @Override
    public Page<MdWorkshop> selectPage(Page<MdWorkshop> page, String workshopCode, String workshopName) {
        Page<MdWorkshop> mdWorkshopPage = workshopMapper.selectPage(page, workshopCode, workshopName);
        return mdWorkshopPage;
    }

    @Override
    public MdWorkshop checkWorkshopById(Long workshopId) {
        MdWorkshop mdWorkshop = workshopMapper.checkWorkshopById(workshopId);
        return mdWorkshop;
    }

    @Override
    public Integer AddOrChWorkshop(MdWorkshop mdWorkshop) {
        //解析jwt获取user信息
        TokenVerifyFilter tokenVerifyFilter = new TokenVerifyFilter();
        if (mdWorkshop.getWorkshopId() != null) {
            //设置更新时间
            mdWorkshop.setUpdateTime(new Date());
            //设置更新者名称
            mdWorkshop.setUpdateBy(tokenVerifyFilter.getUserName().getUsername());
            return workshopMapper.changeMdWorkshop(mdWorkshop);
        } else {
            //设置创建时间
            mdWorkshop.setCreateTime(new Date());
            //设置创建者名称
            mdWorkshop.setCreateBy(tokenVerifyFilter.getUserName().getUsername());
            return workshopMapper.addMdWorkshop(mdWorkshop);
        }
    }

    @Override
    public Integer delWorkshop(Long workshopId) {
        Integer integer = workshopMapper.delWorkshop(workshopId);
        return integer;
    }

    @Override
    public Integer delClientIdArray(List<Long> arrayWorkshopId) {
        Integer integer = workshopMapper.delClientIdArray(arrayWorkshopId);
        return integer;
    }

    @Override
    public List<MdWorkshop> checkAll() {
        List<MdWorkshop> list = workshopMapper.checkAll();
        return list;
    }
}
